package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;


    @Autowired
    private IPermissionService permissionService;
    //处理新增请求
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids) {
        if (role.getId() != null) {
            roleService.update(role,ids);//修改
        } else { //新增
            roleService.save(role,ids);
        }
        return "redirect:/role/list.do";

    }

    //处理去新增请求
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions",permissions);
        //若传 id 值,表示是去修改,那就要去查询被修改的部门对象,存在模型中
        if (id != null) { // 表示去修改
            Role role = roleService.get(id);
            model.addAttribute("role", role);
        }
        // /WEB-INF/views/role/input.jsp
        return "role/input";
    }

    //处理删除部门请求
    @RequestMapping("/delete")
    private String delete(Long id) {
        //传了id值就删除对应的部门数据
        if (id != null) {
            roleService.delete(id);
        }
        return "redirect:/role/list.do";
    }


    // 处理查询所有部门请求
    @RequestMapping("/list")
    public String list(Model model,QueryObject qo) {
        /*PageResult<Role> pageResult = roleService.query(qo);*/
        model.addAttribute("pageInfo", roleService.query(qo));
        return "role/list"; // 找视图 /WEB-INF/views/role/list.jsp
    }
}